package com.vdm.action;

import com.vdm.model.PilotInfo;
import com.vdm.model.UserInfo;

import com.vdm.service.PilotService;
import com.vdm.util.Constants;
import com.vdm.util.ErrorConstants;
import com.vdm.util.ReturnMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/pilot")
public class PilotController {

    @Autowired
    PilotService pilotService;

    @RequestMapping(value = "/toPilotList")
    public String toPilotList(){return "pilot/pilotList";}

    @ResponseBody
    @RequestMapping(value = "/pilotList",method = RequestMethod.GET)
    public Map<String,Object> vehicleList(){
        List<PilotInfo> userList = pilotService.pilotInfoList();
        int count = pilotService.getPilotInfoCount();
        Map<String,Object> retMap = new HashMap<String,Object>();
        retMap.put("code",0);
        retMap.put("msg","");
        retMap.put("count",count);
        retMap.put("data",userList);
        return retMap;
    }

    @ResponseBody
    @RequestMapping(value="/updatePilotStatus",method=RequestMethod.POST)
    public Map<String,Object> updatePilotStatus(@RequestParam(value = "list[]")List<Integer> ids, Integer type){
        if(pilotService.updatePilotInfoStatus(ids, type)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @ResponseBody
    @RequestMapping(value="/updatePilotByPk",method=RequestMethod.POST)
    public Map<String,Object> updatePilotByPk(PilotInfo pilotInfo){
        if(pilotService.updateByPk(pilotInfo)>0){
            return ReturnMapUtil.getOKResult();
        }else{
            return ReturnMapUtil.getErrorResult(ErrorConstants.PARAMETER_ERROR);
        }
    }

    @RequestMapping(value = "/toAddPilot")
    public String toAddPilot(){return "pilot/addPilot";}

    @RequestMapping(value = "/addPilot" , method = RequestMethod.POST)
    public String addPilot(PilotInfo pilotInfo, HttpSession session){
        UserInfo user = (UserInfo) session.getAttribute(Constants.CURRENT_USER);
        pilotInfo.setCreateBy(user.getLoginName());
        pilotService.insertSelective(pilotInfo);
        return "redirect:toPilotList";
    }


}
